package com.kbt.listener;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.google.gson.Gson;
import com.kbt.activity.GoodInfoActivity;
import com.kbt.activity.R;
import com.kbt.base.BaseApplication;
import com.kbt.commont.Constants;
import com.kbt.model.data.AddressListBean;
import com.kbt.model.data.BaseBean;
import com.kbt.model.data.CountBean;
import com.kbt.net.RequestManager;
import com.kbt.util.data.StaticData;
import com.kbt.util.tool.CarCountBadgeUtil;
import com.kbt.util.tool.CommonStatus;
import com.kbt.util.utils.SharePreferenceUtils;
import com.kbt.util.utils.Utils;
import com.readystatesoftware.viewbadger.BadgeView;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhgk
 * @package: com.kbt.listener
 * @TODO: 点击加入购物车按钮触发的事件
 * @data: 2015/8/11 16:45
 * @version: V1.0
 */
public class ClickAddCarListener implements View.OnClickListener,RequestManager.ResponseInterface{
    //周期
    private Activity activity;
    //商品ID
    private String goodId;
    //商品数量文本框
    private EditText goodCountEditText;
    //购物车右上角
    private BadgeView badgeView;

    //商品来源
    private int popWindowFrom;

    private SharePreferenceUtils share;

    private RequestManager mRequestManager;
    private Map<String, String> map =null;
    private Map<String,Object> getMap = null;
    private BaseBean baseBean;
    private String count;//库存的数量
    private CountBean countBean;
    private int buyCount;

    /**
     * 构造函数
     *
     * @param activity
     * @param goodId
     * @param goodCountEditText
     * @param badgeView
     */
    public ClickAddCarListener(Activity activity, String goodId, EditText goodCountEditText, BadgeView badgeView,
                               int popFrom,String count) {
        this.activity = activity;
        this.goodId = goodId;
        this.goodCountEditText = goodCountEditText;
        this.badgeView = badgeView;
        this.popWindowFrom = popFrom;
        mRequestManager = new RequestManager();
        mRequestManager.setResponseListener(this);
        map = new HashMap<String, String>();
        getMap = new HashMap<String,Object>();
        share = new SharePreferenceUtils(activity);
        this.count =count;
    }

    /***
     * @param v
     */
    @Override
    public void onClick(View v) {
         buyCount = Integer.parseInt(String.valueOf(goodCountEditText.getText().toString()));
        if(buyCount>Integer.parseInt(count)){
            Toast.makeText(activity,"库存不足!",Toast.LENGTH_SHORT).show();
            return;
        }
        //获取单个商品的数量,判断
        httpShopNumber();


    }
    //获取单个商品的数量
    public void httpShopNumber(){
        if(Utils.isNetworkAvailable(activity)) {
            mRequestManager.requestGet(Request.Method.GET, Constants.ServiceInterFace.selectShopNumberURL+share.getString("user_mobile","")+"&goods_id="+goodId , Constants.RequestTag.selectShopNumberTag, getMap, CountBean.class);
        }else{
            Toast.makeText(activity,R.string.Wangluo,Toast.LENGTH_SHORT).show();
        }
    }
    private void  httpJoinShopCar(){
        if(Utils.isNetworkAvailable(activity)) {
            map.clear();
            map.put("methodType", "add");
            Gson gson = new Gson();
            Map dataMap = new HashMap<String,String>();
            dataMap.put("user_mobile", share.getString("user_mobile", ""));
            dataMap.put("goods_count", goodCountEditText.getText().toString());
            dataMap.put("goods_id", goodId);
            dataMap.put("access_from", "android");
            map.put("data", gson.toJson(dataMap));
            mRequestManager.post(Constants.ServiceInterFace.joinShopCarURL, map, Constants.RequestTag.joinShopCarTag, BaseBean.class);
        }else {
            Toast.makeText(activity, R.string.Wangluo, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public <T> void successResponse(T parsedGSON, int tag) {
        if (tag == Constants.RequestTag.joinShopCarTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                baseBean = (BaseBean) parsedGSON;
                if(baseBean.getResult().equals("success")){
                    Toast.makeText(activity, "恭喜您，商品已加入到购物车中！", Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(activity,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
        if (tag == Constants.RequestTag.selectShopNumberTag) {
            /** 判断返回值不为空 **/
            if (parsedGSON != null) {
                countBean = (CountBean) parsedGSON;
                if(countBean.getResult().equals("success")){
                   if(countBean.getData()+Integer.parseInt(goodCountEditText.getText().toString())>Integer.parseInt(count)){
                       Toast.makeText(activity,"库存不足!",Toast.LENGTH_SHORT).show();
                       return;
                   }else{
                       CarCountBadgeUtil.setCarBadge(badgeView, buyCount);

                       //回写商品详情的购物车
                       if (popWindowFrom == StaticData.PopWindowFromGoodInfo) {
                           CarCountBadgeUtil.setCarBadge(((GoodInfoActivity) activity).carBadge, buyCount);
                       }

                       httpJoinShopCar();
                   }
                }else{
                    Toast.makeText(activity,""+baseBean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void errorResonse(String retmeg, int tag) {
        Toast.makeText(activity,"error",Toast.LENGTH_SHORT).show();
    }
}
